/*
 * Copyright 2011 Helios Technologies.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.heliostech.gwt.sample.dialog.base.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.heliostech.gwt.widget.dialog.base.BaseDialog;

public class BaseDialogSample implements EntryPoint {

        public interface Resources extends ClientBundle {
                @Source("./resource-add.png")
                ImageResource newContactIcon();
        }
        
        private TextBox fNameF;
        
        @Override
        public void onModuleLoad() {
                final BaseDialog myDialog = new BaseDialog("New Contact", true, true);
                myDialog.setSize("42em", "20em");
                myDialog.setDialogDescription("Create New Contact", 
                                "Fill in the form below with the contact information and click the Save button to create your contact.",
                                ((Resources)GWT.create(Resources.class)).newContactIcon());
                myDialog.setContent(getContent());      
                
                Button saveB = new Button("Save");
                saveB.addClickHandler(new ClickHandler() {
                        @Override
                        public void onClick(ClickEvent event) {
                                if(fNameF.getValue()==null || fNameF.getValue().length()<1) {
                                        myDialog.showError("First name is mandatory.");
                                } else {
                                        Window.alert("You clicked save and your contact will be created!");
                                        myDialog.dismiss();
                                }
                        }
                });
                myDialog.addButton(saveB);
                
                Button cancelB = new Button("Cancel");
                cancelB.addClickHandler(new ClickHandler() {
                        @Override
                        public void onClick(ClickEvent event) {
                                Window.alert("You clicked cancel and nothing will be done!");
                                myDialog.dismiss();
                        }
                });
                myDialog.addButton(cancelB);
                
                
                myDialog.center();
        }
        
        public Widget getContent() {
            // Create a table to layout the form options
            FlexTable layout = new FlexTable();
            layout.setCellSpacing(6);
            
            // First row
            layout.setHTML(0, 0, "First Name");
            fNameF = new TextBox();
            layout.setWidget(0, 1, fNameF);
            layout.setHTML(0, 2, "Last Name");
            layout.setWidget(0, 3, new TextBox());
            // Second row
            layout.setHTML(1, 0, "Address");
            layout.setWidget(1, 1, new TextBox());
            layout.setHTML(1, 2, "Postal Code");
            layout.setWidget(1, 3, new TextBox());
            
            // third row etc...
            layout.setHTML(2, 0, "City");
            layout.setWidget(2, 1, new TextBox());
            layout.setHTML(2, 2, "Country");
            layout.setWidget(2, 3, new TextBox());
            
            return layout;
          }

}